package com.lxu.TreeDemo2;

class TreeNode{
    int value;
    TreeNode node_left;
    TreeNode node_right;

    public TreeNode(int value) {
        this.value = value;
        this.node_left=null;
        this.node_right=null;
    }
}
class BinaryTree {


    public static TreeNode rootNode;
    public static void CreatTree(int value){
        if(rootNode==null){
            rootNode=new TreeNode(value);
        }
        TreeNode currentNode=rootNode;
        while (true){
            if(value<currentNode.value){
                if (currentNode.node_left==null){
                    currentNode.node_left=new TreeNode(value);
                    return;
                }
                else
                    currentNode=currentNode.node_left;
            }
            else {
                if (currentNode.node_right==null){
                    currentNode.node_right=new TreeNode(value);
                    return;
                }
                else
                    currentNode=currentNode.node_right;
            }
        }
    }

    public static void InOrder(TreeNode node){
        if(node!=null){
            InOrder(node.node_left);
            System.out.println(node.value);
            InOrder(node.node_right);
        }
    }

}
public class Test{
    //链表存储二叉树
    public static void main(String[] args){
        int[] a={6,3,9,2,5,7,4,8};

        for (int i=1;i<8;i++){
            BinaryTree.CreatTree(a[i]);
        }
        BinaryTree.InOrder(BinaryTree.rootNode);
    }
}


